package com.mzy.linear_struct.hot100.二叉树;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/09/08/下午7:22
 * @Description:
 */
public class Pro637二叉树的层平均值 {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> ans = new ArrayList<>();
        Deque<TreeNode> deque = new LinkedList<>();

        deque.offer(root);

        while (!deque.isEmpty()) {
            int n = deque.size();
            int len = n;
            double average = 0;
            while (n > 0) {
                TreeNode node = deque.poll();
                System.out.println(node.val);
                average += node.val;
                if (node.left != null) {
                    deque.offer(node.left);
                }
                if (node.right != null) {
                    deque.offer(node.right);
                }

                n--;
            }
            ans.add(average / len);
        }

        return ans;
    }
}

























